#ifndef WSS_SERVER_H
#define WSS_SERVER_H    

#include <functional>
#include <iostream>
#include <list>
#include <mutex>
#include <websocketpp/config/asio.hpp> 
#include <websocketpp/server.hpp>
#include <string>

typedef websocketpp::server<websocketpp::config::asio_tls> server;

using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;

typedef server::message_ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;

class CampusWssServer{
public:
    CampusWssServer(int port);
    virtual ~CampusWssServer();

    int init();
    int send(websocketpp::connection_hdl hdl,const std::string msg);
    int run();
    int wait();

protected:
    virtual void on_open(server* s, websocketpp::connection_hdl hdl);
    virtual void on_close(server* s, websocketpp::connection_hdl hdl);
    virtual void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg);
    virtual void on_fail(websocketpp::connection_hdl hdl) ;
    
    virtual context_ptr on_tls_init();

protected:
    int port_ = 0;
    std::list<websocketpp::connection_hdl> connection_list_;   //连接列表
    std::thread* thread_ = nullptr;
    server server_;
    std::atomic_bool running_ = { false };
    std::mutex mutex_;
};

#endif
